Array Structures in Visual Basic ----------------------------------------------------------------------- After viewing much source code from differnet sources I've found very common programming styles that lend themselves to hogging system resources, and slowing down programs in general. Many beginning programmers also end up making programming much more difficult on themselves in the process. We're going to learn a new word today. ARRAYS ------ Learn it. Live it. Love it. First of all, Arrays make programs run much faster. Everyone likes fast executing programs. Fast is good. Slow is bad. If you like your program to run really slow for no reason, then don't waste your time reading any further. However, if you want to learn how to make your code execute fast and save yourself lots of typing too, read on. Just about any object in Visual Basic can be assigned an Array Index. The Index is an identifier that allows you to access the variable based on a set of conditions. The Array value is set at design time in the (you guessed it) INDEX property. Any object with an Index property can be placed into an Array. So *that's* what Index is for... Now, using Arrays is not designed for every object in a program, but is extremely helpful when the object in question is duplicated several times throughout a program. If your object is the only object of it's type that is accessed the same way, It's not worth assigning Array values to that object. It won't speed anything up or make anything easier if this is the case. Let's look at how much easier it is to control an object with an Array Index than with variables. We need an example that's at least somewhat interesting, so we'll use an Image control that will make our program look a little different than others. We're going to make our own Pop-Up menu, that looks cooler than the "built in" one. Rather than have highlighted options, we're going to use "outlined" options on our Pop-Up menu. To get the "outline" look, we'll simply change the BorderStyle property of a Label control from "0; None", to "1; Fixed Single" when the Mouse Cursor is over the "highlighted" option. We're going to have 10 (ten) different options, simply to illustrate the effects of an Array over coding each label separartely. The higher the number of objects (and their properties) being controlled by the Array, the easier it is on the programmer, and the faster the program will run. For this example, our Pop-Up menu will work when we Click a Command Button on Form1. So, what do we need? Let's see... A start up form (Form1) with a Command Button control (Command1) and our Pop-Up Menu with 10 Label controls that will appear when we Click the Command Button on Form1, and disappear when we select "Cancel". We'll make another form (Form2) and use it for our actual Pop-Up Menu. We need to add 10 Labels onto Form2 and make each Option "do something" when we click on it. For now, we'll make the Command Button Control on our main form display which Option on our Pop-Up Menu was selected, unless it was "Cancel", in which case we'll make the Pop-Up Menu disappear. Here's the code without using an Array: ---------- Private Sub Label1_Click() Form1.Command1.Caption = "Label1" End Sub Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 1 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label2_Click() Form1.Command1.Caption = "Label2" End Sub Private Sub Label2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 1 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label3_Click() Form1.Command1.Caption = "Label3" End Sub Private Sub Label3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 1 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label4_Click() Form1.Command1.Caption = "Label4" End Sub Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 1 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label5_Click() Form1.Command1.Caption = "Label5" End Sub Private Sub Label5_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 1 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label6_Click() Form1.Command1.Caption = "Label6" End Sub Private Sub Label6_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 1 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label7_Click() Form1.Command1.Caption = "Label7" End Sub Private Sub Label7_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 1 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label8_Click() Form1.Command1.Caption = "Label8" End Sub Private Sub Label8_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 1 Label9.BorderStyle = 0 Label10.BorderStyle = 0 End Sub Private Sub Label9_Click() Form1.Command1.Caption = "Label9" End Sub Private Sub Label9_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 1 Label10.BorderStyle = 0 End Sub Private Sub Label10_Click() Label10.BorderStyle = 0 Form1.Command1.Caption = "Click Me" Form2.Visible = False End Sub Private Sub Label10_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.BorderStyle = 0 Label2.BorderStyle = 0 Label3.BorderStyle = 0 Label4.BorderStyle = 0 Label5.BorderStyle = 0 Label6.BorderStyle = 0 Label7.BorderStyle = 0 Label8.BorderStyle = 0 Label9.BorderStyle = 0 Label10.BorderStyle = 1 End Sub ---------- Kinda long huh? Now, let's use an Array and see how much easier it could have been. Both sets of source code accomplish the same thing. ---------- Private Sub Label1_Click(Index As Integer) Form1.Command1.Caption = Label1(Index).Caption If Index = 10 Then Label1(10).BorderStyle = 0 Form1.Command1.Caption = "Click Me" Form2.Visible = False End If End Sub Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) Dim n As Integer For n = 1 To 10 Label1(n).BorderStyle = 0 Next Label1(Index).BorderStyle = 1 End Sub ---------- That's it? Gee whiz Wally! I would have worn out my keyboard coding that without Arrays! Each Label (control) in our Array needs to be named the same thing (Label1) and have an Index number in the Index property field to reference. All we need to do to use the Array, is assign an Index value on each Label control on Form2. The Index number we assign will correspond with each Label in the Array. Note: If you are *changing* an *existing* set of controls, you must first remove the separate controls' code, or you may have problems getting the Visual Basic IDE to correctly name your "new" Array of controls. This happens when the control is named the same as a pre- existing control. Simply remove the old code, which will now be in the Form's "Declarations", before placing any code into the new Array. As a final note, this code only shows how and why to use an Array. If you actually want your own Label Control Menu System, you shouldn't turn "OFF" (set BorderStyle = 0) all of the Labels' BorderStyle properties each time the MouseMove Event is triggered, then turn back "ON" the current one (like we have done in this example, just to make it easy). Even though the coding is much simpler, you'll still be executing many more instructions than are necessary in your program. Try finding which Label is the "current" one and only "turn off" only that one when a new Label is "highlighted", so that your program isn't resetting 10 (or more) property values for each pixel the mouse moves. This defeats half the reason to use the array in the first place (in this example). The Projects HARDWAY.VBP and EASYWAY.VBP are the example files for this tutorial. They both use the same Start Up form (ARRAY.FRM), but different "Menu" forms (HARDWAY.FRM and EASYWAY.FRM). The example files were written in VB4.0 Professional, but should work with any of the Visual Basic for Windows versions (that I'm aware of). The only controls used are a single Command Button and 10 Labels. All example files are archived into ARRAYS.ZIP. Copyright (c) 1997 Russ Ricca russ@spinward.com Content of this text may be freely reproduced in it's complete form.